package com.central.apps.controller;

import com.central.app.service.HandlerService;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//采购订单订单行关闭功能
@RestController
@RequestMapping("/cloButton")
public class cloButtonController {
    @Autowired
    private HandlerService handlerService;
    @PostMapping("/cloButton")
    private String cloButtonController(@RequestParam String ids) throws Exception {
        JSONObject json = new JSONObject();
        String status="已批准";
       String[] values = ids.split(",");
       for (int i=0;i<values.length;i++){
        String id=values[i];
           List<Map<String, Object>> bySql1 = handlerService.findBySql("select poid from gq_posline where id='" + id + "'");
           if (CollectionUtils.isNotEmpty(bySql1)){
               String poid = MapUtils.getString(bySql1.get(0), "poid");
               List<Map<String, Object>> bySql2 = handlerService.findBySql("select id,shutreason from gq_posline where poid='" + poid + "'");
               for (int j=0;j<bySql2.size();j++){
                   Map<String, Object> map = bySql2.get(j);
                   String id1 = MapUtils.getString(map, "id");
                   if (id1.equals(id)){
                       //查看采购商接受是否选择该订单
                       List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_prtsummaryline where poslid='" + id + "'");
                       if (CollectionUtils.isNotEmpty(bySql)){
                           int y=j+1;
                           json.put("code", "1");
                           json.put("msg", "该采购订单第"+y+"行已做采购接收，无法关闭!");
                           return json.toString();
                       }else {
                           //查看采购业务发票是否选择该订单
                           List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_invoicepursummary where poslid='" + id + "'");
                           if (CollectionUtils.isNotEmpty(bySql3)){
                               int y=j+1;
                               json.put("code", "1");
                               json.put("msg", "该采购订单第"+y+"行已做业务发票，无法关闭!");
                               return json.toString();
                           }
                       }
                       //已经关闭的订单无法在关闭
                       String shutreason = MapUtils.getString(map, "shutreason");
                       if (shutreason!=null&&shutreason!=""){
                           int y=j+1;
                           json.put("code", "1");
                           json.put("msg", "已经关闭的订单无法再次关闭!");
                           return json.toString();
                       }
                   }
               }

           }

       }
        json.put("code", "0");
        json.put("msg", "关闭成功!");
        return json.toString();
    }
}
